<template>
  <div class="else-detail-new">
    <div class="one" v-for="item,index in list" :key="index">
      <b><img src="../../../../public/img/email/icon/icon-file.png" alt="">{{name}}</b>
      <span class="more">{{item.picName}} ({{dealSize(item.fileSize)}})
        <span style="color: #8C8C8C;margin-left: 15px" v-show="item.hasOwnProperty('expire')">{{item.expire}} 过期</span></span>
      <a :href="item.fileGuid" target="_blank">
        <el-button size="small" type="text">预览</el-button>
      </a>
      <a href="javascript:void(0)" title="重命名" @click="editName(index)" v-show="name=='附件'">
        <el-button size="small" type="text">重命名</el-button>
      </a>
      <a href="javascript:void(0)" title="删除附件" @click="delRow(item)">
        <el-button size="small" type="text">删除</el-button>
      </a>
    </div>
  </div>
</template>

<script>
    export default {
        name: "MailFile",
        props: ['list','name'],
        methods: {
            editName(index){
                const old = this.list[index].picName;
                this.$prompt(old, '附件重命名', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    // inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
                    inputErrorMessage: '名称字符长度不超过50'
                }).then(({ value }) => {
                    this.list[index].picName=value;
                    console.log(this.list)
                }).catch(() => {});
            },
            delRow(item) {
                this.$emit('delText', item)
            },
            //转换文件单位
            dealSize(size) {
                size = size.toString();
                if (size.indexOf('KB') != -1 || size.indexOf('B') != -1 || size.indexOf('MB') != -1
                    || size.indexOf('kb') != -1 || size.indexOf('b') != -1 || size.indexOf('mb') != -1) {
                    return size;
                }
                let result = parseInt(size / 1024);
                if (result > 1024) {
                    return parseInt(result / 1024) + 'M';
                }
                return result + 'KB';
            }
        }
    }
</script>

<style  lang="scss">
  @import "../../../styles/file-review";
</style>
